package com.metadata_system.api.inspect;


import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.exception.ApiException;
import com.metadata_system.common.exception.ApiSignException;
import com.metadata_system.common.service.CommonConfigService;
import com.metadata_system.common.utils.ApiSignUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Aspect
@Configuration
@Slf4j
public class AiApiCheckInterceptor {

    @Autowired
    CommonConfigService commonConfigService;
    @Value("${spring.profiles.active:local}")
    String springProfilesActive;

    @SneakyThrows
    @Around("execution(public * *(..)) && @annotation(com.metadata_system.api.annotations.AiSystemApiCheck)")
    public Object interceptor(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
//        Method method = signature.getMethod();

        // 1.获取 request
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        if (StringUtils.isBlank(request.getHeader("ApiKey"))) {
            throw new ApiException("缺少关键信息 1");
        }
        if (StringUtils.isBlank(request.getHeader("Timestamp"))) {
            throw new ApiException("缺少关键信息 2");
        }
        if (StringUtils.isBlank(request.getHeader("Sign"))) {
            throw new ApiException("缺少关键信息 3");
        }
        String logicToolsApiKey = this.commonConfigService.getContentByKey("AI_SYSTEM_API_KEY");
        String logicToolsApiSecret = this.commonConfigService.getContentByKey("AI_SYSTEM_API_SECRET");
        if (!request.getHeader("ApiKey").equals(logicToolsApiKey)) {
            throw new ApiException("ApiKey错误");
        }
        // 2.检查签名
        if (springProfilesActive.equals("prod") || (request.getHeader("CheckSign") != null && request.getHeader("CheckSign").equalsIgnoreCase("true"))) {
            ApiResult apiResult = ApiSignUtils.aiSystemCheckSign(request.getRequestURL().toString(),
                    request.getHeader("ApiKey"),
                    request.getHeader("Timestamp"),
                    request.getHeader("Sign"),
                    logicToolsApiSecret,
                    request.getParameterMap());
            if (apiResult.isError()) {
                throw new ApiSignException("Api签名检查错误: " + apiResult.getMessage());
            }
        }
        return pjp.proceed();
    }


}
